package kr.kimkisik.middle1math.gyesan;

import kr.kimkisik.middle1math.R;
import kr.kimkisik.middle1math.danwon.DanwonMain;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

public class Jungngak2 extends Activity implements MobileAdListener {
	private MobileAdView adView = null;
	EditText mEditN;
	TextView mTextResult;
	double pi = 3.14;
	double mN = 0;
	InputMethodManager imm;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.gyesan_jungngag2);

		mEditN = (EditText) findViewById(R.id.jungn2_r);
		mTextResult = (TextView) findViewById(R.id.jungn2_result);
		button = (Button) findViewById(R.id.jungn2_gyesangogo);
		adView = (MobileAdView) findViewById(R.id.adview1);
		adView.setListener(this);
		adView.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy(); // 10
			adView = null;
		}
	}

	@Override
	public void onReceive(int err) {
		// event for receive ad
	}

	public void mOnClick(View v) {
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		try {
			mN = Double.parseDouble(mEditN.getText().toString());
		} catch (Exception e) {
			Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
					Toast.LENGTH_SHORT).show();
		}
		switch (v.getId()) {
		case R.id.jungn2_gyesangogo:
			if (mN == 0) {
				Toast.makeText(this, "숫자를 입력하지 않으셨거나, \n 잘못입력하셨습니다.",
						Toast.LENGTH_SHORT).show();
			} else {
				imm.hideSoftInputFromWindow(button.getWindowToken(), 0);
				mTextResult.setText("▶ 정n각형의 n : " + mEditN.getText() + "\n☞정"
						+ mEditN.getText() + "각형의 한 내각의 \n크기 = "
						+ (int) ((180 * (mN - 2) / mN) * 100 + 0.5f) / 100f);
				mTextResult.setTextColor(Color.WHITE);
			}
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {

		case R.id.danwonmenu:
			intent.setClass(this, DanwonMain.class);
			startActivity(intent);
			return true;

		case R.id.gyesan:
			intent.setClass(this, GyesanMain.class);
			startActivity(intent);
			return true;

		}
		return false;
	}

}
